/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
*/

package plar.core;

import plar.core.Action;

/**
 * This Action is in each Element by default, it is for auto-destruction of
 * TEMPORARY objects: - read the float Element.lifeTime (in seconds) - ask to
 * Level how many run() should be long this time - call DestroyAction after this
 * time.
 *
 * @author Antonio Ragagnin
 *
 */

public class ActionTimer extends Action {
    int time = -2;
    public String runAction;
    public boolean repeat;
    public float lt1;
    public float lt2;
    public boolean flag;
    public boolean active;
    public ActionTimer(String an,float t1,float t2,boolean r)
    {

        runAction=an;
        lt1=t1;
        lt2=t2;
        repeat=r;
        flag=true;
        active=true;

    }
    public void run() {

        if(time==-2 && lt1!=0 && flag==true) time = (Integer) me.level.frames(lt1);
        else if(time==-2 && lt1==0 && flag==true)time = (Integer) me.level.frames(me.lifeTime);
        if(time==-2 && lt1!=0 && flag==false) time = (Integer) me.level.frames(lt2);
        else if(time==-2 && lt1==0 && flag==false)time = 0;


        if (time == 0) {
            flag=!flag;
            drin();
            if(repeat) time=-2;

        } else if(time>0 && active) time--;




    }
    public void drin()
    {
        if(runAction==null)
            me.actions.getAction(runAction).force(null);
        else
            force(null);
    }

    public void setTime(float t)
    {
        lt1=t;
        time = (Integer) me.level.frames(lt1);
    }
    public void setTimer(boolean b)
    {
        repeat=b;
    }

}
